package com.wangwei.routercompiler;

import com.wangwei.annotationrouter.Factory;


import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.MirroredTypeException;

public class FactoryAnotatedClass {
    // 别注解的类元素
    private TypeElement annotatedClassElement;
    // 全名 eg android.view.View
    private String qualifedSuperClassName;
    // 类名 eg Activity
    private String simpleTypeName;
    // 注解的参数
    private String id;

    public FactoryAnotatedClass(TypeElement classElement) throws IllegalArgumentException {
        this.annotatedClassElement = classElement;
        Factory factory = classElement.getAnnotation(Factory.class);
        id = factory.id();
        if (id == null || (id != null && id.trim().length() == 0)) {
            throw new IllegalArgumentException(String.format("id() in @%s from clsas %s is null or empty that is not allowed",
                    Factory.class.getSimpleName(), classElement.getQualifiedName().toString()));
        }
        try {
            Class<?> clazz = factory.type();
            // getCanonicalName 包括包名 类的全路径
            qualifedSuperClassName = clazz.getCanonicalName();
            simpleTypeName = clazz.getSimpleName();

        } catch (MirroredTypeException mte) {
            //代码没有被编译过
            DeclaredType declaredType = (DeclaredType) mte.getTypeMirror();
            TypeElement classTypeElement = (TypeElement) declaredType.asElement();
            qualifedSuperClassName = classTypeElement.getQualifiedName().toString();
            simpleTypeName = classTypeElement.getSimpleName().toString();
        }


    }

    public String getQualifedSuperClassName() {
        return qualifedSuperClassName;
    }

    public void setQualifedSuperClassName(String qualifedSuperClassName) {
        this.qualifedSuperClassName = qualifedSuperClassName;
    }

    public String getSimpleTypeName() {
        return simpleTypeName;
    }

    public void setSimpleTypeName(String simpleTypeName) {
        this.simpleTypeName = simpleTypeName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public TypeElement getAnnotatedClassElement() {
        return annotatedClassElement;
    }
}
